package com.huike.web.controller.clues;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.dto.TbActivityDTO;
import com.huike.clues.service.TbActivityService;
import com.huike.clues.service.impl.TbActivityServiceImpl;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.bean.BeanUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 活动管理Controller
 * @date 2023-04-01
 */
@RestController
@RequestMapping("/clues/activity")
public class TbActivityController extends BaseController {
    @Autowired
    private TbActivityService tbActivityService;

    @GetMapping("/list")
    public TableDataInfo list(TbActivity tbActivity) {
        startPage();
        List<TbActivity> list = tbActivityService.selectbyid(tbActivity);
        return getDataTable(list);
    }

    @PostMapping
    public AjaxResult add(@RequestBody TbActivityDTO dto) {
        String uuid = UUID.randomUUID().toString().substring(0, 8);
        dto.setCode(uuid);
        dto.setStatus(String.valueOf(2));
        dto.setCreateTime(new Date());
        TbActivity activity = new TbActivity();
        // 属性映射逻辑
        BeanUtils.copyProperties(dto, activity);

        tbActivityService.save(activity);
        return success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public AjaxResult listbyid(@PathVariable Integer id) {
        TbActivity byId = tbActivityService.getById(id);
        return AjaxResult.success(byId);
    }
    @PutMapping
    public AjaxResult update(@RequestBody TbActivityDTO dto){
        TbActivity tbActivity = new TbActivity();
        BeanUtils.copyProperties(dto, tbActivity);
        tbActivityService.updateById(tbActivity);
        return success();
    }
    /**
     * 删除活动
     */
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable String ids){
        tbActivityService.removeById(ids);
        return success();
    }
    /**
     * 获取状态为2的活动
     */
    @GetMapping("/listselect/{channel}")
    public AjaxResult listselect(@PathVariable String channel){
        List<TbActivity> list = tbActivityService.lambdaQuery().eq(channel != null, TbActivity::getChannel, channel)
                .list();
        return AjaxResult.success(list);
    }

}
